package com.xyzpool.test;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexpTestor {
	public static void testMatch(String regexp,String... targets){
		System.out.println("TEST PATTERN "+regexp);
		Pattern pattern = Pattern.compile(regexp);
		for (int i = 0; i < targets.length; i++) {
			Matcher mat = pattern.matcher(targets[i]);
			if(mat.matches()){
				System.out.println(targets[i]+" OK!");
			}else{
				System.out.println(targets[i]+" FAILURE!");
			}
		}
		System.out.println();
	}
	/**
	 * Pattern.compile("[a-zA-Z]{1,3}").matcher("AMZ1903459").group(); 这个有问题，需要先通过match或find找到对应参数，然后再能使用Group返回找到对象.
	 * @param regexp
	 * @param targets
	 */
	public static void testFind(String regexp,String... targets){
		System.out.println("MATCH PATTERN "+regexp);
		Pattern pattern = Pattern.compile(regexp);
		for (int i = 0; i < targets.length; i++) {
			Matcher mat = pattern.matcher(targets[i]);
			if(mat.find()){
				String str = mat.group();
				String strs = mat.replaceAll("");
				System.out.println(strs+" OK! "+str+" finded.");
			}else{
				System.out.println(targets[i]+" FAILURE!");
			}
		}
		System.out.println();
	}
	public static void main(String[] args) {
//		String[] strs = 
//		{
//				null,
//				"",
//				"400-17109 (X2), CPF21A02",
//				"352-20558-10-BK, 352-20724-10",
//				"378-31295-12, CPJ16F05+378-31295-11, CPR16A01",//SKU1, LOC1+SKU2, LOC2
//				"313-44144-02, H08G+313-44144-01, CPT03D03",//SKU1, LOC1(without CP)+ SKU2, LOC2
//				"GC10084A1L+GC10084A1R, CPQ13B01",//SKU1+SKU2, LOC2
//				"GC10084A1L+GC10084A1R, CPQ13B01",//SKU1+SKU2, LOC2
//				"352-13592-10-PM, CPB14C05",//SKU, LOC
//				"JBA-105A, H03A",//SKU,LOC without CP
//				"378-36313-22+378-36313-21",//SKU1,SKU2
//				"ORYH3269B-NE95"//SKU
//		};
//		testMatch("[^,+]*(,[ ]*(CP)?[a-zA-Z][0-9]{2,2}[a-zA-Z]([0-9]{2,2})?)?([+][^,+]*(,[ ]*(CP)?[a-zA-Z][0-9]{2,2}[a-zA-Z]([0-9]{2,2})?)?)*",strs);
//		testMatch("[^,]+,( )*[^,()([a-zA-Z][0-9]{2,2}[a-zA-Z])]+",strs);
//		testMatch("[^+,]*,( )*[^,]+",strs);
//		testMatch("[^+,]*,( )*(CP)?[a-zA-Z][0-9]{2,2}[a-zA-Z][0-9]{2,2}",strs);
//		testMatch("[^+,]*,( )*(CP)?[a-zA-Z][0-9]{2,2}[a-zA-Z]",strs);

//		testMatch("[((CP)?[a-zA-Z][0-9]{2,2}[a-zA-Z][0-9]{2,2})([a-zA-Z][0-9]{2,2}[a-zA-Z])]",new String[]{"CPT03D03","H03A"});
		//String[] strs2 = {", CPB14C05"};
		//testFit(strs2,", CP[^,+]*");
		//String[] strs3 = {"378-36313-22","378-36313-22+378-36313-21"};
		//testFit("[^,+]*(, CP[^,+]*)?",strs3);
		//test match
//		String[] testStr = {"371-36359-01, CPG27F02","378-31295-12, CPJ16F05+378-31295-11, CPR16A01"};
//		testMatch(", CP[^,+].*[+]?",testStr);
		
//		String[] str = {"CP1903501(6)","SS1903480(NA)","AP1903562(NA)+AP1903563(NA)","AP1903562(NA)+AP1903563","AP1903562+AP1903563","AP1903562+AP1903563(NA)","AMZ1903459"};
//		testFit("^[a-zA-Z]{1,3}[\\d]+([(].*[)])?([+][a-zA-Z]{1,3}[\\d]+([(].*[)])?)*$",str);
//		Matcher matcher = Pattern.compile("\\d+").matcher("AMZ1903459");
//		matcher.find();
//		System.out.println(matcher.group());
//		if(matcher.matches()){
//			System.out.println(matcher);
//		}else{
//			System.out.println("FAilure!");
		//testMatch("^.*[ ][(]X2[)]$", new String[]{"400-17109 (X2)","352-20558-10-BK","378-36313-22","400-171(X2)09","400-9(171X20)","400-9(171X20)(X20)"});
		String strs[] = {"400-17109 (X2), CPF21A02",
				"80-20177 (X2), CPF17E05",
				"HN10089B1L (X2), CPQ05F01",
				"80-20177 (X2), CPF17E05",
				"352-20128-10-BK (X2)"};
		Pattern pat = Pattern.compile("^.*[ ][(]X2[)]$");
		Matcher matcher = pat.matcher(strs[i]);
		if(matcher.matches()){
			System.out.println();
		}else{
			System.out.println(str);
		}
	}
}
